package com.hgl.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
 * 
 * @author:HGL
 * ClassName:com.hgl.filter.CharsetFilter.java
 * Date:2020年8月6日下午8:02:57
 * Version:1.0
 */

public class CharsetFilter implements Filter{
	private String charset="UTF-8";
	private String contentType="text/html;charset=UTF-8";
	@Override
	public void destroy() {
		
	}
	@Override
	public void doFilter(ServletRequest requset, ServletResponse response,
			FilterChain arg2) throws IOException, ServletException {
		if(requset.getCharacterEncoding()==null){
			requset.setCharacterEncoding(charset);
		}
		if(response.getContentType()==null){
			response.setContentType(contentType);
		}
	}
	@Override
	public void init(FilterConfig config) throws ServletException {
		if(config.getInitParameter("charset")!=null){
			charset=config.getInitParameter("charset");
		}
		if(config.getInitParameter("contentType")!=null){
			contentType=config.getInitParameter("contentType");
		}
	}
	
	
	

}
